/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exercises.day04.pt01.dao;

import exercises.day04.pt01.bean.AccountRegistration;
import exercises.day04.pt02.Exception.AccountTypeIsInvalidException;
import exercises.day04.pt02.Exception.AccountTypeIsNullException;
import exercises.day04.pt02.Exception.AddressIsNullException;
import exercises.day04.pt02.Exception.AddressLenghtLessThan25Exception;
import exercises.day04.pt02.Exception.AnnualIncomeIsNullException;
import exercises.day04.pt02.Exception.AnnualIncomeValidationException;
import exercises.day04.pt02.Exception.FirstNameIsNullException;
import exercises.day04.pt02.Exception.IncomeIsNotDoubleException;
import exercises.day04.pt02.Exception.LastNameIsNullException;
import exercises.day04.pt02.Exception.NameIsJohnDoeException;
import exercises.day04.pt02.Exception.PhoneIsNullException;
import exercises.day04.pt02.Exception.PhoneLenghtLessThan7Exception;
import exercises.day04.pt02.Exception.RegisterIdIsNotIntegerException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Student
 */
public class Account_Registration_DAO extends Connector {

    public Account_Registration_DAO() {
    }

    public boolean register(AccountRegistration account) {
        try {
            //INSERT INTO ROOT.REGISTRATION (CFIRST_NAME, CLAST_NAME, CADDRESS, CACCOUNT_TYPE, MANNUAL_INCOME, CPHONE_NO) VALUES (5, 'Laurry', 'Helbert', 'Rock St.Seattle 234657', 'Fixed Deposit                 ', 450000.0, '0416348');

            PreparedStatement ps = this.getConn().prepareStatement("INSERT INTO ROOT.REGISTRATION (CREGISTRATION_ID, "
                    + "CFIRST_NAME, CLAST_NAME, CADDRESS, CACCOUNT_TYPE, MANNUAL_INCOME, CPHONE_NO) "
                    + "VALUES (?,?,?,?,?,?,?)");
            ps.setInt(1, account.getRegId());
            ps.setString(2, account.getfName());
            ps.setString(3, account.getlName());
            ps.setString(4, account.getAddress());
            ps.setString(5, account.getAccountType());
            ps.setDouble(6, account.getIncome());
            ps.setString(7, account.getPhone());
            ps.executeUpdate();

        } catch (SQLException ex) {
            System.out.println(ex.toString());

            return false;
        }

        return true;
    }

    public ArrayList<AccountRegistration> searchAccountType(String accountType) {

        ArrayList<AccountRegistration> found = new ArrayList<AccountRegistration>();

        try {

            AccountRegistration ar = null;
            
            PreparedStatement ps = this.getConn().prepareStatement("select * from ROOT.REGISTRATION where CACCOUNT_TYPE = ? ");

            ps.setString(1, accountType);

            ResultSet rs = ps.executeQuery();

            //STEP 5: Iterate through the results set

            while (rs.next()) {
                ar = new AccountRegistration(rs.getString("CREGISTRATION_ID"),
                        rs.getString("CFIRST_NAME").trim(),
                        rs.getString("CLAST_NAME"),
                        rs.getString("CADDRESS"),
                        rs.getString("CACCOUNT_TYPE").trim(),
                        rs.getString("MANNUAL_INCOME"),
                        rs.getString("CPHONE_NO"));
                found.add(ar);
            }


        } catch (AccountTypeIsInvalidException ex) {
            Logger.getLogger(Account_Registration_DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (AccountTypeIsNullException ex) {
            Logger.getLogger(Account_Registration_DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (AddressIsNullException ex) {
            Logger.getLogger(Account_Registration_DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (AddressLenghtLessThan25Exception ex) {
            Logger.getLogger(Account_Registration_DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FirstNameIsNullException ex) {
            Logger.getLogger(Account_Registration_DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (LastNameIsNullException ex) {
            Logger.getLogger(Account_Registration_DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NameIsJohnDoeException ex) {
            Logger.getLogger(Account_Registration_DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (AnnualIncomeIsNullException ex) {
            Logger.getLogger(Account_Registration_DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (AnnualIncomeValidationException ex) {
            Logger.getLogger(Account_Registration_DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (PhoneIsNullException ex) {
            Logger.getLogger(Account_Registration_DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (PhoneLenghtLessThan7Exception ex) {
            Logger.getLogger(Account_Registration_DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RegisterIdIsNotIntegerException ex) {
            Logger.getLogger(Account_Registration_DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IncomeIsNotDoubleException ex) {
            Logger.getLogger(Account_Registration_DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Account_Registration_DAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return found;
    }
}
